<?php
// +----------------------------------------------------------------------
// |店铺管理
// +----------------------------------------------------------------------
// |Data:2016-06-24 下午4:58
// +----------------------------------------------------------------------
// |Author: 吕志雄 <870923001@qq.com>
// +----------------------------------------------------------------------
namespace Common\Model;
class ShopModel extends CommonModel{
    protected $tableName = 'shop';
    private $shop_detial_cache_key = 'sp_shop_detail_';
    private $shop_list_cache_key = 'sp_shop_list_';

    /**
     * 获取详情
     * @param 编号ID $id
     * @return bool|mixed
     */
    public function detail($id,$field = true){
        if(empty($id)) return false;
        $detail = S($this->shop_detial_cache_key.$id);
        if(!$detail){
            $detail = $this->where('id='.$id)->find();
            S($this->shop_detial_cache_key.$id,$detail);
        }
        if($field === true){
            return $detail;
        }else if($field){
            return $detail[$field];
        }
    }

    /**
     * 检测是否有分类
     * @param $cate_id
     * @return bool|mixed
     */
    public function hasCate($cate_id){
        if(empty($cate_id)) return false;
        return $this->where('cate_id='.$cate_id)->getField('id');
    }

    public function update($data){
        if(empty($data) || !is_array($data)) return false;
        $result = parent::update($data);
        if($result){
            if($_SESSION['administrator']){
                $uid = C('MANAGE_id');
            }else{
                $uid = $data['uid'];
            }
            $this->clearCache($data['id'],$uid);
        }
        return $result;
    }

    /**
     * 判断是否有商品
     * @param $id 店铺ID
     * @return bool|mixed
     */
    public function hasProject($id){
        if(empty($id)) return false;
        return M('project')->where('shop_id='.$id)->getField('uid');
    }

    /**
     * 删除
     * @param 编号ID $id
     * @param $uid
     * @return bool
     */
    public function del($id,$uid){
        if(empty($id)) return false;
        $res = parent::del($id);
        if($res){
            $this->clearCache($id,$uid);
        }
        return $res;
    }

    /**
     * 获取用户店铺列表
     * @param $uid
     * @return bool|mixed
     */
    public function getShopList($uid){
        if(empty($uid)) return false;
        $map = array();
        if($uid == C('MANAGE_id')){
            $map['id'] = array('gt',0);
        }else{
            $map['uid'] = $uid;
        }
        $list = S($this->shop_list_cache_key.$uid);
        if(!$list){
            $list = $this->where($map)->field('id,title')->select();
            S($this->shop_list_cache_key.$uid,$list);
        }
        return $list;
    }

    /**
     * 获取前台店铺列表
     * @param $map
     * @param $ord
     * @return bool
     */
    public function getHomeShopList($map,$ord){
        if(empty($map) || !is_array($map)) return false;
        $list = $this->where($map)->field('id,pic,title,location,desc,uid,sales_time,deviler_price,free_price')->order($ord)->select();
        return $list;
    }

    /**
     * 清理缓存
     * @param $id
     */
    protected function clearCache($id,$uid=''){
        !empty($uid) && S($this->shop_list_cache_key.$uid,null);
        !empty($id) && S($this->shop_detial_cache_key.$id,null);
    }
}
?>